#include "mainwindow.h"
#include "equ_include/humidity_temperature.h"
#include "ui_mainwindow.h"
#include "equ_include/ECS_FCC300.h"
#include "QMessageBox"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    canset=new CANset();
    Interface_Rtimer=new QTimer(this);
    Timer_10s=new QTimer(this);
    FaultListModel= new QStringListModel(this);
    Rest_count=0;
    StartTime=QTime::currentTime();
    FaultListModel->setStringList(FaultList);
    ui->listView_fault_list->setModel(FaultListModel);
    Interface_Init();
    Equ_runinfo_init();
    connect(Interface_Rtimer,SIGNAL(timeout()),this,SLOT(Data_Refresh()));
    connect(Interface_Rtimer,SIGNAL(timeout()),this,SLOT(Interface_Refresh()));
    connect(Timer_10s,SIGNAL(timeout()),this,SLOT(Auto_Runlog_info()));
    connect(this,SIGNAL(Displayrefresh_signal(int)),this,SLOT(Fault_show()));
    connect(this,SIGNAL(Displayrefresh_signal(int)),this,SLOT(EQRun_loglist()));
    //connect(this,SIGNAL(Displayrefresh_signal(int)),mqttset,SLOT(mqtt_sed()));
}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::Interface_Init()      //界面配置初始化
{
    Interface_rt=200;
    Timer_10s_rt=10000;//log记录时间
    Interface_Rtimer->stop();
    Interface_Rtimer->setInterval(Interface_rt);//毫秒
    Timer_10s->stop();
    Timer_10s->setInterval(Timer_10s_rt);//毫秒
    Interface_Rtimer->start();
    Timer_10s->start();
    rest_par_1=0;
    rest_par_2=0;
    rest_par_3=0;
    rest_par_4=0;
    CAN_send_control_0=0;
    CAN_send_control_1=0;
    CAN_send_control_2=0;
    CAN_send_control_3=0;
    Save_Data=0;
    fileName="";
    SetRunTime_BIT=0;
    SetRunTime=0;
    ECS_FCC300::rest_data_info();
    ECS_FCC300::rest_data_ma();

}




void MainWindow::Equ_runinfo_init()    //设备运行数据初始化
{

    ui->tableView_eq01->setModel(&ECS_FCC300_mode_info);
}

void MainWindow::Hid_fault_Init()
{


}


void MainWindow::Data_Refresh()            //数据刷新函数
{
    ECS_FCC300::EQ_runinfo();
    ui->tableView_eq01->viewport()->update();

}


void MainWindow::Protect_fun()
{

}

void MainWindow::HidFault_Scan()           //故障扫描
{

}


void MainWindow::Interface_Refresh()       //界面刷新函数
{
    emit Displayrefresh_signal(1);
    NowTime = QTime::currentTime();
    int t = StartTime.msecsTo(NowTime)/1000;
    QDateTime curDataTime=QDateTime::currentDateTime();
    ui->editDateTime->setText(curDataTime.toString("yyyy-MM-dd hh:mm:ss"));
    ui->LCDHour->display((int) t/360000);
    ui->LCDMin->display((int) (t/60)%60);
    ui->LCDSec->display(t%60);
    qDebug()<<"ECS_FCC300_ma.AIR_Status="<<ECS_FCC300_ma.AIR_Status;



    if(SetRunTime_BIT==1)
    {
        NowTime_SetRun= QTime::currentTime();
        int t_run=StartTime_SetRun.msecsTo(NowTime_SetRun)/1000;
        ui->LCDMin_SetRunTime->display(QString::number(SetRunTime-t_run));
        qDebug()<<"t_run="<<t_run;
        if(SetRunTime-t_run==0)
    {
            if(ECS_FCC300_ma.AIR_Status== 0x0b)
            {
                ECS_FCC300_ma.AIR_Status=0x05;
                ui->radioButton_CAPEnable_run->setChecked(0);
                ui->radioButton_CAPEnable_Disable->setChecked(1);
                SetRunTime_BIT=0;
                ui->pushButton_2->setText("开始");
            }
        StartTime_SetRun.isNull();
        qDebug()<<"t_run="<<t_run;
    }

    }


    ui->label_SetRunTime->setText(QString::number(SetRunTime));
    float t_dex;
    t_dex=ECS_FCC300_ma.AIR_Speed_Set*100/95000;
    qDebug()<<t_dex;
    ui->progressBar->setValue(t_dex);
    ui->label_Speed_Command->setText(QString::number(ECS_FCC300_ma.AIR_Speed_Set)+"rpm");
    ui->label_ECS_FCC300_info_Feedback_speed->setText(QString::number(ECS_FCC300_info.Speed)+"rpm");
    ui->label_ECS_FCC300_info_Busbar_current->setText(QString::number(ECS_FCC300_info.Phase_Current)+"A");
    ui->label_ECS_FCC300_info_Busbar_voltage->setText(QString::number(ECS_FCC300_info.Bus_Voltage)+"V");
    ui->label_ECS_FCC300_info_Motor_current->setText(QString::number(ECS_FCC300_info.Motor_Current)+"A");
    ui->label_ECS_FCC300_info_Motor_voltage->setText(QString::number(ECS_FCC300_info.Motor_Voltage)+"kw");
    ui->label_ECS_FCC300_info_controller_temperature->setText(QString::number(ECS_FCC300_info.Controller_TEMP)+"℃");
    ui->label_ECS_FCC300_info_Compressor_temperature->setText(QString::number(ECS_FCC300_info.Motor_TEMP)+"℃");
    ui->label_ECS_FCC300_info_Motor_Power->setText(QString::number(ECS_FCC300_info.Motor_Power)+"KW");
    ui->label_ECS_FCC300_info_SoftVersion->setText(QString::number(ECS_FCC300_info.Compressor_Mode));
    ui->label_ECS_FCC300_info_Compressor_model->setText(QString::number(ECS_FCC300_info.Soft_Version));
    ui->label_ECS_FCC300_info_temp_1->setText(QString::number(ECS_FCC300_info.temp_1)+"℃");
    ui->label_ECS_FCC300_info_temp_2->setText(QString::number(ECS_FCC300_info.temp_2)+"℃");
    ui->label_EQ_0x1834A7D3_TEMP->setText(QString::number(Hum_Temp_1834A7D3.TEMP)+"℃");
    ui->label_EQ_0x1834A7D3_Humidity->setText(QString::number(Hum_Temp_1834A7D3.Humidity)+"%");

}
void MainWindow::Fault_show()
{
    FaultList.clear();
    ECS_FCC300::Fault_processing();
    FaultList.append(ECS_FCC300_info.FaultList);
    FaultListModel->setStringList(FaultList);
    ui->listView_fault_list->viewport()->update();//故障列表刷新
}

void MainWindow::EQRun_loglist()
{


}


void MainWindow::save_data()
{


}

void MainWindow::Auto_Runlog_info()
{


}

void MainWindow::ADD_Savedata_Titlebar()
{


}

void MainWindow::ADD_Savedata_Line()
{


}



void MainWindow::on_checkBox_mancotr_stateChanged(int arg1)
{
    if(arg1==2)
    {
        CAN_send_control_0=1;

    }
    else
    {
        CAN_send_control_0=0;

    }
}


void MainWindow::on_radioButton_CAPEnable_Disable_clicked()
{
        ECS_FCC300_ma.AIR_Status=0x05;
}


void MainWindow::on_radioButton_CAPEnable_run_clicked()
{
     ECS_FCC300_ma.AIR_Status=0x0b;
}


void MainWindow::on_radioButton_CAPEnable_FR_clicked()
{
    ECS_FCC300_ma.AIR_Status=0xfe;
}


void MainWindow::on_pushButton_CAP_set_clicked()
{
    ECS_FCC300_ma.AIR_Speed_Set=ui->doubleSpinBox_CAP_SpeedCommand->value();
    ui->verticalSlider->setValue(ECS_FCC300_ma.AIR_Speed_Set);
}


void MainWindow::on_actionCAN_triggered()
{
    canset->show();
}


void MainWindow::on_actionactionMQTT_triggered()
{

}


void MainWindow::on_pushButton_time_clear_clicked()
{
        StartTime=QTime::currentTime();
}


void MainWindow::on_pushButton_faultclear_clicked()
{
    ECS_FCC300_info.FaultList.clear();
    FaultList.clear();
}


void MainWindow::on_verticalSlider_actionTriggered(int action)
{
    if(ui->verticalSlider->value()<30000 && 0<ui->verticalSlider->value() )
    {
        ECS_FCC300_ma.AIR_Speed_Set=30000;
    }
    else
    {
         ECS_FCC300_ma.AIR_Speed_Set=ui->verticalSlider->value();

    }
}





void MainWindow::on_pushButton_clicked()
{

    SetRunTime=ui->spinBox_SetRunTime->value();
}


void MainWindow::on_pushButton_2_clicked()
{
    if(ui->radioButton_CAPEnable_run->isChecked()==1 and SetRunTime>0)
    {
        if(SetRunTime_BIT==0)
        {
            SetRunTime_BIT=1;
            StartTime_SetRun=QTime::currentTime();
            ui->pushButton_2->setText("暂停");

        }
        else
        {
            SetRunTime_BIT=0;
            ui->pushButton_2->setText("开始");
        }
    }

else

    {
        QMessageBox::information(this, QStringLiteral("提示"), QStringLiteral("需空压机为运行状态或定时时长大于0"));
    }



}


void MainWindow::on_pushButton_3_clicked()
{

    StartTime_SetRun=QTime::currentTime();
}

